﻿// -----------------------------------------------------------------------
// <copyright file="AtomRocket.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace SpaceshipDuel
{
    using System;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AtomRocket : GameObject
    {
        #region Private fields

        private Vector2 velocity; // Geschwindigkeitsvektor
        private bool destroyMe;
        protected float rotationAngle; // Rotationswinkel

        #endregion Private fields

        #region Public properties

        public Vector2 Velocity
        {
            get { return this.velocity; }
            set { this.velocity = value; }
        }

        public int Radius
        {
            get { return textureData.halfheight; }
        }

        public bool DestroyMe
        {
            get { return destroyMe; }
            set { this.destroyMe = value; }
        }

        public float RotationAngle
        {
            get { return this.rotationAngle; }
            set { this.rotationAngle = value; }
        }

        #endregion Public properties

        #region Construction

        /// <summary>
        /// Konstruktor für Atomrakete
        /// </summary>
        /// <param name="newCenter">Positionsvektor Raumschiff</param>
        /// <param name="newWorldSize">Größe des Spielfeldes</param>
        /// <param name="newSpeed">Geschwindigkeit</param>
        public AtomRocket(Vector2 newCenter, int newWorldSize, float newSpeed, float newRotationAngle)
        {
            textureData = TextureProvider.TexAtomRocket;
           
            worldSize = newWorldSize;
            rotationAngle = newRotationAngle;
            velocity = new Vector2(0, 0);
            Vector2 myDirection = new Vector2((float)Math.Sin(rotationAngle), (float)-Math.Cos(rotationAngle));
            myDirection.Normalize();
            center = newCenter + myDirection * (32 + 25);
            velocity.X = (float)(myDirection.X * newSpeed);
            velocity.Y = (float)(myDirection.Y * newSpeed);
            destroyMe = false;
        }

        #endregion Construction

        #region Public methods

        /// <summary>
        /// Draw-Methode
        /// </summary>
        /// <param name="spriteBatch"></param>
        /// <param name="xOffset"></param>
        /// <param name="yOffset"></param>
        new public void Draw(SpriteBatch spriteBatch, float xOffset, float yOffset)
        {
            spriteBatch.Draw(textureData.texture, new Vector2(center.X + xOffset, center.Y + yOffset), new Rectangle(0, 0, textureData.width, textureData.height), Color.White, rotationAngle, new Vector2(textureData.halfwidth, textureData.halfheight), 1.0f, SpriteEffects.None, 0f);
        }

        /// <summary>
        /// Update-Methode
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            center += velocity;
            CheckPosition();
        }

        #endregion Public methods

        #region Protected methods

        /// <summary>
        /// Korrektur der Raumschiffposition beim Flug über Spielfeldgrenzen hinweg
        /// </summary>
        protected void CheckPosition()
        {
            if (center.X < 0)
                center.X = worldSize + center.X;
            else if (center.X >= worldSize)
                center.X = center.X - worldSize;

            if (center.Y < 0)
                center.Y = worldSize + center.Y;
            else if (center.Y >= worldSize)
                center.Y = center.Y - worldSize;
        }

        #endregion Private methods
    }
}
